# Create your views here.

from django.views import View

from model.models import User
from server.utils import Response, Request, LoginRequiredSuperAdmin, Verify, Code


class UserView(LoginRequiredSuperAdmin, View):
    """ Query user information """

    def get(self, request):
        """ Get user information """
        uid = request.GET.get('uid')
        role = request.GET.get('role')
        status = request.GET.get('status')

        users = User.objects.all()
        if uid:
            users = users.filter(uid__contains=uid)
        if role and int(role) in User.Role:
            users = users.filter(role=role)
        if status and int(status) in User.Status:
            users = users.filter(status=status)

        users = [{**user.info(), **user.pointStatistics()} for user in users]
        return Response.Json(users=users)

    @Request.getJson
    def post(self, request):
        """ Modifying User Personal Information """
        phone = request.JSON.get('phone')

        if phone != None:
            code = Verify.phone(phone)
            if code != Code.OK:
                return Response.Json(code=code)
            request.user.phone = phone

        request.user.save()
        return Response.Json()
